//
//  MainViewController.swift
//  仿一个新浪微博
//
//  Created by 孙家林 on 15/9/9.
//  Copyright © 2015年 sunjialin. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()

    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        setUpComposedButton()
    }
    
    ///运行循环监听到时间后，向VC发送消息，动态执行方法，因此不能设置位 private
    func clickComposedButton(){
        
        ///        print(__FUNCTION__)
        //根据用户是否登录来决定是否能够显示发布微博界面
        let vc = UserAccount.userLogin ? ComposeViewController() : OAuthViewController()
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    ///设置按钮位置
    private func setUpComposedButton(){
        
        let width = tabBar.bounds.width / CGFloat ((viewControllers?.count)!)
        
        let rect = CGRect(x: 0, y: 0, width: width, height: tabBar.bounds.height)
        
        composeButton.frame = CGRectOffset(rect, 2 * width, 0)
        
    }
    
    ///添加所有子控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UITableViewController())
        
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    ///添加子控制器
    private func addChildViewController(vc:UIViewController, title:String, imageName:String) {
        
        tabBar.tintColor = UIColor.orangeColor()
        
        ///        let vc = HomeTableViewController()
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    ///懒加载
    lazy private var composeButton:UIButton = {
        
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        
        button.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()

}
